<?php

namespace App\Kuafu\Shop\Resources\B;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ShopPaymentsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return $this->camelize(array_merge([
            'id'     => $this->id,
            'shopId' => $this->shopId,
        ], json_decode($this->payConfig, true)));
    }

    /**
     * 键 转驼峰
     *
     * @param array $data
     * @param string $separator
     * @return array
     */
    public function camelize(array $data, string $separator = '_'): array
    {
        $return = [];
        collect($data)->each(function ($value, $key) use (&$return, $separator) {
            if (strpos($key, $separator) !== false) {
                $newKey = ltrim(str_replace(
                    " ",
                    "",
                    ucwords($separator . str_replace($separator, " ", strtolower($key)))),
                    $separator
                );
            } else {
                $newKey = $key;
            }
            $return[$newKey] = $value;
        });
        return $return;

    }

}
